package trastienda.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Categoria;
import trastienda.modelo.Producto;


@Repository
public class CategoriaDAOImpl implements CategoriaDAO{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	@SuppressWarnings(value = "unchecked")
	public Collection<Categoria> listarCategorias() throws DAOExcepcion {
        
        String sql = "select id_categoria,nombre,descripcion from categoria";

        RowMapper mapper = new RowMapper() {
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                        Categoria cat = new Categoria();
                        cat.setIdCategoria(rs.getInt("id_categoria"));
                        cat.setNombre(rs.getString("nombre"));
                        cat.setDescripcion(rs.getString("descripcion"));
                        
                        return cat;
                }
        };
        return jdbcTemplate.query(sql, mapper);
}
	
	
	@SuppressWarnings(value = "unchecked")
	public Categoria insertarCategoria(Categoria cat) throws DAOExcepcion{
		System.out.println("insertar categoria");
		String query = "INSERT INTO categoria (id_categoria,nombre,descripcion)"
				+ "VALUES (?,?,?)";
		Object[] params = new Object[] {cat.getIdCategoria(), cat.getNombre(),cat.getDescripcion()};
		try {
			jdbcTemplate.update(query, params);
		} catch (Exception e) {
			throw new DAOExcepcion(e.getMessage());
		}
		return cat;
	}
	
	@SuppressWarnings("unchecked")
	public Categoria obtenerPorIdCategoria(int idCategoria) throws DAOExcepcion {
		System.out.println("CategoriaDAOImpl: obtenerPorIdCategoria() : "	+ idCategoria);

		String sql = "SELECT id_categoria, nombre, descripcion FROM Categoria where id_categoria=?";

		RowMapper mapper = new RowMapper() {

			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Categoria cat = new Categoria();
				
				cat.setIdCategoria(rs.getInt("id_categoria"));
				cat.setNombre(rs.getString("nombre"));
				cat.setDescripcion(rs.getString("descripcion"));
				
								
				return cat;
			}
		};
		return (Categoria) jdbcTemplate.queryForObject(sql, new Object[] {idCategoria }, mapper);
	}
	
	@SuppressWarnings(value = "unchecked")
	public Categoria modificarCategoria(Categoria cat) throws DAOExcepcion{
		System.out.println("modificar categoria");
		String query = "update categoria set id_categoria=?,nombre=?, descripcion=? where id_categoria="+cat.getIdCategoria();
		Object[] params = new Object[] {cat.getIdCategoria(), cat.getNombre(),cat.getDescripcion()};
		try {
			jdbcTemplate.update(query, params);
		} catch (Exception e) {
			throw new DAOExcepcion(e.getMessage());
		}
		return cat;
	}
	
	@SuppressWarnings(value = "unchecked")
	public Categoria eliminarCategoria(Categoria cat) throws DAOExcepcion{
		System.out.println("eliminar categoria");
		String query = "DELETE FROM categoria WHERE id_categoria=?";
		Object[] params = new Object[] {cat.getIdCategoria()};
		try {
			jdbcTemplate.update(query, params);
		} catch (Exception e) {
			throw new DAOExcepcion(e.getMessage());
		}
		return cat;
	}
	
	

}
